package com.ems.sys.service.impl;

import com.ems.sys.dao.EmsLeaveMapper;
import com.ems.sys.pojo.EmsLeave;
import com.ems.sys.service.EmsLeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:EmsLeave业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class EmsLeaveServiceImpl implements EmsLeaveService {

    @Autowired
    private EmsLeaveMapper emsLeaveMapper;


    /**
     * EmsLeave条件+分页查询
     * @param emsLeave 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<EmsLeave> findPage(EmsLeave emsLeave, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(emsLeave);
        //执行搜索
        return new PageInfo<EmsLeave>(emsLeaveMapper.selectByExample(example));
    }

    /**
     * EmsLeave分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<EmsLeave> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<EmsLeave>(emsLeaveMapper.selectAll());
    }

    /**
     * EmsLeave条件查询
     * @param emsLeave
     * @return
     */
    @Override
    public List<EmsLeave> findList(EmsLeave emsLeave){
        //构建查询条件
        Example example = createExample(emsLeave);
        //根据构建的条件查询数据
        return emsLeaveMapper.selectByExample(example);
    }


    /**
     * EmsLeave构建查询对象
     * @param emsLeave
     * @return
     */
    public Example createExample(EmsLeave emsLeave){
        Example example=new Example(EmsLeave.class);
        Example.Criteria criteria = example.createCriteria();
        if(emsLeave!=null){
            //
            if(!StringUtils.isEmpty(emsLeave.getEId())){
                    criteria.andEqualTo("eId",emsLeave.getEId());
            }
            //
            if(!StringUtils.isEmpty(emsLeave.getEName())){
                    criteria.andEqualTo("eName",emsLeave.getEName());
            }
            //
            if(!StringUtils.isEmpty(emsLeave.getLeaveDate())){
                    criteria.andEqualTo("leaveDate",emsLeave.getLeaveDate());
            }
            //
            if(!StringUtils.isEmpty(emsLeave.getBackDate())){
                    criteria.andEqualTo("backDate",emsLeave.getBackDate());
            }
            //
            if(!StringUtils.isEmpty(emsLeave.getLeaveReason())){
                    criteria.andEqualTo("leaveReason",emsLeave.getLeaveReason());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        emsLeaveMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改EmsLeave
     * @param emsLeave
     */
    @Override
    public void update(EmsLeave emsLeave){
        emsLeaveMapper.updateByPrimaryKey(emsLeave);
    }

    /**
     * 增加EmsLeave
     * @param emsLeave
     */
    @Override
    public void add(EmsLeave emsLeave){
        emsLeaveMapper.insert(emsLeave);
    }

    /**
     * 根据ID查询EmsLeave
     * @param id
     * @return
     */
    @Override
    public EmsLeave findById(String id){
        return  emsLeaveMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询EmsLeave全部数据
     * @return
     */
    @Override
    public List<EmsLeave> findAll() {
        return emsLeaveMapper.selectAll();
    }
}
